package com.pheruagor.handmanager.model {
    import com.pheruagor.handmanager.model.business.Club;
    import com.pheruagor.handmanager.model.business.CustomMenuItem;
    import com.pheruagor.handmanager.storage.impl.sqlite.ClubSQLiteStorage;

    import mx.collections.ArrayCollection;

    import org.puremvc.as3.patterns.proxy.Proxy;

    public class ApplicationProxy extends Proxy {
        public static const NAME:String = "applicationProxy";

        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        private var m_clubs:ArrayCollection;
        private var m_currentClub:Club;
        private var m_clubStorage:ClubSQLiteStorage;

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------
        public function ApplicationProxy(data:Object = null) {
            super(NAME, data);
        }

        override public function onRegister():void {
            m_clubStorage = new ClubSQLiteStorage();
            m_clubs = new ArrayCollection();
            listExistingClubs();
        }

        //-----------------------------------------------------
        // Access
        //-----------------------------------------------------
        public function get currentClub():Club {
            return m_currentClub;
        }

        public function set currentClub(value:Club):void {
            m_currentClub = value;
        }

        public function get clubs():ArrayCollection {
            return m_clubs;
        }

        //-----------------------------------------------------
        // Operation
        //-----------------------------------------------------
        public function listMenuItems():ArrayCollection {
            var result:ArrayCollection = new ArrayCollection();
            var fileMenu:CustomMenuItem = new CustomMenuItem("file", "Fichier", new ArrayCollection());
            result.addItem(fileMenu);
            // open sub menu
            var openSubMenu:CustomMenuItem = new CustomMenuItem("Open", "Ouvrir", new ArrayCollection());
            fileMenu.children.addItem(openSubMenu);
            var openClubSubMenu:CustomMenuItem = new CustomMenuItem("openClub", "Club");
            openSubMenu.children.addItem(openClubSubMenu);

            var exitSubMenu:CustomMenuItem = new CustomMenuItem("exit", "Quitter");
            fileMenu.children.addItem(exitSubMenu);

            return result;
        }

        /**
         * Return existing club
         * TODO: add real persistance layer access
         */
        public function listExistingClubs(onResult:Function = null, onFault:Function = null):void {
            var resultHandler:Function = function(result:Array):void {
                    m_clubs = new ArrayCollection();
                    for each (var club:Club in result) {
                        m_clubs.addItem(club);
                    }
                    if (onResult != null) {
                        onResult(m_clubs);
                    }
                }
            var faultHandler:Function = function():void {
                }
            m_clubStorage.listClubs(resultHandler, faultHandler);
        }

        public function addClub(club:Club):void {
            var resultHandler:Function = function():void {
                    m_clubs.addItem(club);
                }
            var faultHandler:Function = function():void {

                };
            m_clubStorage.saveClub(club, resultHandler, faultHandler);
        }

        public function removeClub(club:Club):void {
            if (club == null) {
                throw new ArgumentError("club cannot be null");
            }
            var resultHandler:Function = function():void {
                    m_clubs.removeItemAt(m_clubs.getItemIndex(club));
                }
            m_clubStorage.removeClub(club, resultHandler);
        }
        //-----------------------------------------------------
        // Implementation
        //-----------------------------------------------------
    }
}